package com.jluzh10.learning.web.controller;

import com.jluzh10.learning.domain.Teacher;
import com.jluzh10.learning.domain.User;
import com.jluzh10.learning.service.TeacherService;
import com.jluzh10.learning.utils.ResponseData;
import com.jluzh10.learning.web.converter.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author yzg
 * @create 2019/5/27
 *
 * 教学方
 */
@Controller
@RequestMapping("teacher")
public class TeacherController {

    private final TeacherService teacherService;

    @Autowired
    public TeacherController(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    /**
     * 根据id查询教学方信息
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData selectOneById(@PathVariable(value = "id") Integer teacherId){

        Teacher teacher = teacherService.selectOneById(teacherId);
        if (teacher!=null){
            return new ResponseData().success().data(teacher);
        }
        return new ResponseData().fail();
    }

    /**
     * 进入教学方管理界面
     * @param modelAndView
     * @return
     */
    @RequestMapping(value = "/personal")
    public ModelAndView toTeacher(@LoginUser User user, ModelAndView modelAndView){

        modelAndView.setViewName("teacher");
        modelAndView.addObject("teacherId",user.getId());
        return modelAndView;
    }
}
